home *** CD-ROM | disk | FTP | other *** search
- Path: seas.smu.edu!not-for-mail
- From: dbowman@post.smu.edu (Damon Bowman)
- Newsgroups: comp.lang.c++
- Subject: precision methods
- Date: 26 Jan 1996 18:01:11 -0600
- Organization: Southern Methodist University
- Sender: usenet@seas.smu.edu
- Message-ID: <4ebq07$54r@sun.cis.smu.edu>
- Reply-To: dbowman@post.smu.edu
- NNTP-Posting-Host: sun.cis.smu.edu
- X-Nntp-Posting-Host: ax4-22.ppp.smu.edu
- X-Newsreader: Forte Free Agent 1.0.82
-
-
- I get a strange compiler warning when I try to compile this code.
- It's from a C++ textbook by Deitel/Deitel.
-
- The compiler warning follows the code. What is the source of this
- warning message?
-
- I know compiler warnings are off-topic for this newsgroup, but I also
- have a C++ language question:
-
- How is the precision member functions supposed to work? The book says
- that it controls the number of places to the RIGHT of the decimal. In
- other words,
-
- float a = 45.67;
- cout.precision(2);
- cout << a;
-
- Should output 45.67. It actually outputs 46 on my machine. Is the
- book wrong (wouldn't be the first time I've found blatant errors in
- programming books).
-
- Here's the sample code in the text, followed by the compiler warning:
-
- //fig11_17.cpp
- //Controlling precision of floating-point values
- #include <iostream.h>
- #include <iomanip.h>
- #include <math.h>
-
- main()
- {
- double root2 = sqrt(2.0);
- cout << "Square root of 2 with precisions 0-9." << endl
- << "Precision set by the precision member function:" << endl;
-
- for (int places = 0; places <= 9; places++)
- {
- cout.precision(places);
- cout << root2 << endl;
- }
- cout << endl << "Precision set by the setprecision manipulator:" <<
- endl;
-
- for (places = 0; places <=9; places++)
- cout << setprecision(places) << root2 << endl;
- return 0;
- }
-
- d:\msvc\test\fig11_17.cpp(21) : warning C4270: 'initializing' : do not
- initialize a non-const 'class ::__SMANIP_int __near &' with a
- non-lvalue 'class ::__SMANIP_int ' function return
-
-